package com.p2p.front.dal.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.model.Transfer;
import com.p2p.front.enums.TransferStatus;

/**
 * @author ruiqi
 *
 */
public interface TransferDao extends BaseMapper{
	
	void create(Transfer transfer);
	
	@Update("update p2p_transfer set tid=#{tid},uid=#{uid},ruid=#{ruid},luid=#{luid},lid=#{lid},title=#{title},img=#{img},trans_principal=#{transPrincipal.cent},trans_amount=#{transAmount.cent},trans_interest=#{transInterest.cent},trans_fee=#{transFee.cent},trans_num=#{transNum},transpwd=#{transpwd},status=#{status},exptime=#{exptime},addtime=#{addtime},uptime=#{uptime},oktime=#{oktime}  where id=#{id}")
	int update(Transfer transfer);
	
	@Update("update p2p_transfer set trans_amount=#{transAmount.cent},trans_interest=#{transInterest.cent},uptime=#{uptime}  where id=#{id}")
	void updateInterestAmount(Transfer lastetTransfer);
	
	@Update("update p2p_transfer set status=#{status},uptime=now() where id=#{id} and uid=#{uid} and status=#{oldStatus}")
	int updateStatus(@Param("id")Long id,@Param("uid")Long uid,@Param("oldStatus")Integer oldStatus,@Param("status")Integer status);
	
	@Update("update p2p_transfer set status=#{status} where id=#{id}")
	int simpleUpdateStatus(@Param("id")Long id,@Param("status")Integer status);
	
	Transfer getTransfer(Transfer transfer);
	
	@Transactional(propagation=Propagation.MANDATORY)
	Transfer getTransferForUpdate(Transfer transfer);
	
	List<Transfer> getTransferListByLidForUpdate(Transfer transfer);

	List<Transfer> getTransferListByTidListForUpdate(@Param("tidList")List<Long> tidList,@Param("status") Integer status);
	
	List<Map<String,Object>> listPage(PageVo form);

	List<Map<String,Object>> searchListPage(PageVo form);
	
	@Select("select * from p2p_transfer where id=#{transferid}")
	Map<String, Object> get(Long transferid);

	@Select("select * from p2p_transfer where uid=#{uid} and status=11")
	List<Map<String,Object>> findTransfering(String uid);

	@Select("select count(*) from p2p_transfer where tid=#{tid} and status=#{status}")
	Integer count(@Param("tid") Long tid, @Param("status")Integer status);


	
}
